package com.quik.web.product.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.quik.entity.DtaProduct;
import com.quik.entity.DtaProductCategory;
import com.quik.entity.DtaProductSubCategory;
import com.quik.web.product.mapper.ProductDao;

/**
 * 功能说明：
 * 			接口ProductService的实现类
 * @author Jenny
 *
 */
@Service
public class ProductServiceImpl implements ProductService {
	
	@Autowired
	private ProductDao dao;
	
	/**
	 * 查询分类的总数
	 * @param categoryName
	 * @return
	 * @throws Exception
	 */
	@Override
	public int findCategoryCount(String categoryName) throws Exception{
		int count = dao.findCategoryCount(categoryName);
		
		return count;
	}
	
	/**
	 * 根据商品名称查询商品分类
	 * @param categoryName
	 * @param intPage
	 * @param pageSize
	 * @return
	 * @throws Exception
	 */
	@Override
	public List findCategory(String categoryName, int intPage, int pageSize) throws Exception{
		List list = dao.findCategory(categoryName, intPage, pageSize);
		if(list != null && list.size() > 0){
			Integer[] ids = new Integer[list.size()];
			for (int i = 0; i < list.size(); i++) {
				DtaProductCategory pc = (DtaProductCategory)list.get(i);
				ids[i] = pc.getId();
			}
			List subList = dao.findSubCategory(ids);
			if(subList != null && subList.size() > 0)
				list.addAll(subList);
		}

		return list;
	}
	
	/**
	 * 查询商品分类
	 * @param categoryName
	 * @param intPage
	 * @param pageSize
	 * @return
	 * @throws Exception
	 */
	public List findCategory() throws Exception{
		List list = dao.findCategory();
		return list;
	}
	
	/**
	 * 是否已存在相同商品类别
	 * @param categoryName
	 * @return
	 * @throws Exception
	 */
	@Override
	public boolean isExistCategory(String categoryName) throws Exception{
		int count = dao.findCategoryCountByCategoryName(categoryName);
		
		if(count > 0)
			return true;
		else
			return false;
	}
	
	/**
	 * 新增商品大类
	 * @param category
	 * @throws Exception
	 */
	@Override
	public void addCategory(DtaProductCategory category) throws Exception{
		dao.addCategory(category);
	}
	
	/**
	 * 同一大类下是否已存在相同商品子类
	 * @param categoryName
	 * @return
	 * @throws Exception
	 */
	@Override
	public boolean isExistSubCategory(String subCategoryName, Integer categoryId) throws Exception{
		int count = dao.findSubCategoryCountBySubCategoryName(subCategoryName, categoryId);
		
		if(count > 0)
			return true;
		else
			return false;
	}
	
	/**
	 * 新增商品子类
	 * @param category
	 * @throws Exception
	 */
	@Override
	public void addSubCategory(DtaProductSubCategory subCategory) throws Exception{
		dao.addSubCategory(subCategory);
	}
	
	/**
	 * 根据条件查询商品总数
	 * @param categoryId
	 * @param subCategoryId
	 * @param productNo
	 * @param productName
	 * @return
	 * @throws Exception
	 */
	@Override
	public int findProductCount(Integer categoryId, Integer subCategoryId, String productNo, String productName) throws Exception{
		int count = dao.findProductCount(categoryId, subCategoryId, productNo, productName);
		
		return count;
	}
	
	/**
	 * 根据条件查询商品列表
	 * @param categoryId
	 * @param subCategoryId
	 * @param productNo
	 * @param productName
	 * @return
	 * @throws Exception
	 */
	@Override
	public List findProduct(Integer categoryId, Integer subCategoryId, String productNo, String productName,
			int intPage, int pageSize) throws Exception{
		List<DtaProduct> list = dao.findProduct(categoryId, subCategoryId, productNo, productName, intPage, pageSize);
		
		return list;
	}
	
	/**
	 * 根据类别ID查询子类列表
	 * @param categoryId
	 * @return
	 * @throws Exception
	 */
	public List findSubCategoryByCategoryId(Integer categoryId) throws Exception{
		List list = dao.findSubCategoryByCategoryId(categoryId);
		return list;
	}
	
	/**
	 * 检查是否有重复的商品编码
	 * 
	 * @return 如果重复 返回true, 否则false
	 */
	@Override
	public boolean checkProductNo(String productNo) throws Exception{
		int count = dao.findProductNoCount(productNo);
		
		if(count > 0)
			return true;
		else
			return false;
	}
	
	/**
	 * 新增商品
	 * @param product
	 * @throws Exception
	 */
	@Override
	public void addProduct(DtaProduct product) throws Exception{
		dao.addProduct(product);
	}

	@Override
	public List<DtaProduct> findAllProduct() {
		return this.dao.findAllProduct();
	}

	@Override
	public List<DtaProductCategory> findAllProductCategory() {
		return this.dao.findAllProductCategory();
	}

	@Override
	public List<DtaProductSubCategory> findAllproductSubCategory() {
		return this.dao.findAllproductSubCategory();
	}
}
